﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.Product;
using ECommerce.Model.Product;
using Jzy.Core.Dict;

namespace ECommerce.Web.Site
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        protected int ProductID
        {
            get
            {
                return int.Parse(Request.QueryString["ProductID"]);
            }
        }

        private ECProductBO Product
        {
            get
            {
                return ECProductBO.Instance<ECProductBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DispInfo();
            }
        }


        protected string GetScore(object score)
        {
            int avgScore = 0;
            if (score is int)
            {
                avgScore = (int)Math.Round((double)(int)score);
            }
            else
            {
                avgScore = (int)Math.Round((double)score);
            }
            string star = "";
            for (int i = 0; i < avgScore; i++)
            {
                star += "<img src='images/tbg1.jpg' />";
            }
            return star;
        }

        //protected string FirstImage;
        //protected string OriginImage;
        private void DispInfo()
        {
            ECProductInfo info = Product.GetInfo(ProductID);
            //if(info.Pictures.Count>0)
            //{
            //    FirstImage = info.Pictures.First().BigImageAbsPath;
            //    OriginImage = info.Pictures.First().OriginImageAbsPath;
            //}

            litID.Text = info.ID.ToString();
            litType1Name.Text = litMake.Text = info.Type1Name;
            Name.Text = litProductName.Text = info.Name;
            Remark.Text = Remark2.Text = info.Remark;
            OldPrice.Text = info.OldPrice.ToString("c2");
            CurrentPrice.Text = info.CurrentPrice.ToString("c2");

            Material.Text = Material2.Text = info.Material;
            Packag.Text = Packag2.Text = Packag3.Text = info.Packag;
            FlowerWord.Text = info.FlowerWord;
            SendRange.Text = info.SendRange;
            imgAD.ImageUrl = info.AdImageAbsPath;

            litCommentImg.Text = GetScore(info.AvgScore);
            litCommentCount.Text = "(" + info.Comments.Count + "条)";

            rpt.DataSource = info.Pictures;
            rpt.DataBind();

            rptBigImage.DataSource = info.Pictures;
            rptBigImage.DataBind();
          


            DictBO dict = new DictBO();

            IEnumerable<int> size = info.Size.Split(',').Select(T => int.Parse(T));

            List<DictInfo> list = dict.GetList(size);
            drpSize.DataSource = list;
            drpSize.DataTextField = "Name";
            drpSize.DataValueField = "Name";
            drpSize.DataBind();


            rptComment.DataSource = info.Comments;
            rptComment.DataBind();
            lblCommentCount.Text = info.Comments.Count.ToString();


            lnkFavorite.NavigateUrl = "AddFavorite.aspx?ProductID=" + this.ProductID;

                
        }

       

        protected void btnBuy_Click(object sender, EventArgs e)
        {
            string count = txtCount.Text;
            string size = drpSize.SelectedValue;

            Response.Redirect("PutCart.aspx?ProductID=" + ProductID + "&ProductCount=" + count + "&ProductSize=" + Server.UrlEncode(drpSize.SelectedValue));
        }

        protected void btnFavorite_Click(object sender, EventArgs e)
        {
            //this.Product.AddToFavorite(Site
        }
    }
}